fn main() {
    struct TwoNumber {
        num1: f32,
        num2: f32,
    }

    impl TwoNumber {
        // 执行给定操作的函数，返回运算结果或错误信息
        fn operate<F>(&self, op: F) -> Result<f32, &'static str>
        where
            F: Fn(f32, f32) -> Result<f32, &'static str>,
        {
            op(self.num1, self.num2)
        }
    }

    // 这是一个用于执行加法运算的函数，接受两个 f32 类型的参数，并返回它们的和或错误信息。
    fn add(a: f32, b: f32) -> Result<f32, &'static str> {
        Ok(a + b)
    }

    fn sub(a: f32, b: f32) -> Result<f32, &'static str> {
        Ok(a - b)
    }

    fn mul(a: f32, b: f32) -> Result<f32, &'static str> {
        Ok(a * b)
    }

    fn div(a: f32, b: f32) -> Result<f32, &'static str> {
        if b == 0.0 {
            Err("division by zero")
        } else {
            Ok(a / b)
        }
    }

    let num1 = TwoNumber {
        num1: 10.0,
        num2: 0.0,
    };

    // 调用operate函数, 传入函数add
    let result1 = num1.operate(add);
    let result2 = num1.operate(sub);
    let result3 = num1.operate(mul);
    let result4 = num1.operate(div);

    // 打印结果
    println!("{:?}", result1);
    println!("{:?}", result2);
    println!("{:?}", result3);
    println!("{:?}", result4);
}
